package gewu.suleju.controller;

import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.vo.PageInfo;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.IHouseInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("housepage")
public class HousePageController {
    @Autowired
    private IHouseInforService iHouseInforService;
    @Autowired
    private PageInfo pageInfo;
    @Autowired
    private RedisTemplate redisTemplate;
    @ResponseBody
    @GetMapping("page")
    public Result page(HttpServletRequest request,@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "2") int pageSize){
        Object isSelect = redisTemplate.opsForValue().get(pageNum);
        if(isSelect == null) {
            Page<HouseInfor> page = iHouseInforService.findPageAll(pageNum, pageSize);
            pageInfo.setDataInfor(page.getContent());
            pageInfo.setPageCount((int) page.getTotalPages());
            pageInfo.setPageNum((pageNum));
            pageInfo.setPageSize(page.getSize());
            pageInfo.setPageTotal((int) page.getTotalElements()+1);
//            System.out.println(pageInfo.getPageTotal());
//            System.out.println(pageInfo.getPageSize());

            redisTemplate.opsForValue().set(pageNum,pageInfo,10, TimeUnit.SECONDS);//将第一次查询出来的东西放在redis，防止平凡访问数据库
        }
        else {
            pageInfo = (PageInfo)redisTemplate.opsForValue().get(pageNum);

        }
        return new Result("200","房子分页",pageInfo);
    }
}